package com.burnerpat.fl2d.math;

public class Vector2D
{
	public double x = 0.0f;
	public double y = 0.0f;

	public Vector2D()
	{

	}

	public Vector2D(double x, double y)
	{
		this.x = x;
		this.y = y;
	}

	public Vector2D(double startX, double startY, double endX, double endY)
	{
		this(endX - startX, endY - startY);
	}

	public Vector2D(Vector2D start, Vector2D end)
	{
		this(start.x, start.y, end.x, end.y);
	}

	public Vector2D(Vector2D other)
	{
		this(other.x, other.y);
	}

	public static Vector2D unityVector()
	{
		return new Vector2D(0, -1.0);
	}

	public double getAngle()
	{
		double a = FastMath.atan(FastMath.abs(y) / FastMath.abs(x));

		if (x < 0.0)
		{
			return (y < 0.0) ? a : (a + FastMath.PIHALF);
		}
		else
		{
			return (y > 0.0) ? (a + FastMath.PI)
					: (a + FastMath.PI + FastMath.PIHALF);
		}
	}

	public void setAngle(double angle)
	{
		setAngle(angle, length());
	}

	public void setAngle(double angle, double length)
	{
		x = FastMath.cos(angle) * length;
		y = FastMath.cos(angle) * length;
	}

	public double length()
	{
		return FastMath.sqrt(x * x + y * y);
	}
	
	public double sqrLength()
	{
		return (x * x + y * y);
	}
	
	public Vector2D neg()
	{
		return new Vector2D(-x, -y);
	}
	
	public Vector2D abs()
	{
		return new Vector2D(FastMath.abs(x), FastMath.abs(y));
	}
	
	public Vector2D add(Vector2D other)
	{
		return new Vector2D(x + other.x, y + other.y);
	}
	
	public Vector2D sub(Vector2D other)
	{
		return add(other.neg());
	}
	
	public Vector2D mul(double d)
	{
		return new Vector2D(x * d, y * d);
	}
	
	public Vector2D delta(Vector2D other)
	{
		return new Vector2D(this, other).abs();
	}
	
	public boolean equals(Object obj)
	{
		if (!(obj instanceof Vector2D))
		{
			return false;
		}
		
		Vector2D other = (Vector2D)obj;
		
		return (x == other.x) && (y == other.y);
	}
}
